package com.example.springboot.service;

import com.example.springboot.common.Page;
import com.example.springboot.entity.User;
import com.example.springboot.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能：通过@Service将类放到Spring容器里
 * 作者：ljr
 * 日期：2023/11/3 10:03
 */
@Service
public class UserService {
    @Autowired
    UserMapper userMapper;

    public void insertUser(User user){
        userMapper.insert(user);

    }

    public void updateUser(User user) {
        userMapper.update(user);
    }

    public void deleteUser(Integer id) {
        userMapper.delete(id);
    }

    public void batchdeleteUser(List<Integer> ids) {
        for(Integer id : ids){
            userMapper.delete(id);
        }
    }

    public List<User> selectAll() {
        return userMapper.selectAll();
    }

    public User selectById(Integer id) {
        return userMapper.selectById(id);
    }

    public List<User> selectByMore(String name, String username) {
        return userMapper.selectByMore(name,username);
    }

    public List<User> selectByMo(String name, String username) {
        return userMapper.selectByMo(name,username);
    }

    public Page<User> selectByPage(Integer pageNum, Integer pageSize, String name, String username) {
        //页码
        Integer skipNum = (pageNum-1) * pageSize;
        Page<User> page = new Page<>();
        //分页查询，skipNum当前页，pageSize每页查询的个数
        List<User> userList=userMapper.selectByPage(skipNum,pageSize,name,username);
        //统计查询到多少条数据
        Integer total=userMapper.selectCountByPage(name,username);
        page.setTotal(total);
        page.setList(userList);

        return page;
    }
}
